package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sky.context.ThreadLocalUtil;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public List<ShoppingCart> findListByShoppingCart(ShoppingCart shoppingCart) {
       List<ShoppingCart> list = shoppingCartMapper.findByUserId(shoppingCart.getUserId());
        return list;
    }

    @Override
    public void addShoppingCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = BeanUtil.copyProperties(dto, ShoppingCart.class);
        shoppingCart.setUserId(ThreadLocalUtil.getCurrentId());
        ShoppingCart exist = shoppingCartMapper.findExist(shoppingCart);
        if(exist!=null){
            exist.setNumber(exist.getNumber()+1);
            shoppingCartMapper.updateNumber(exist);
        }else {
            if(shoppingCart.getDishId()!=null){
                Dish dish = dishMapper.findById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setDishFlavor(dto.getDishFlavor());
            }else {
                Setmeal setmeal = setmealMapper.findById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.save(shoppingCart);
        }

    }

    @Override
    public void cleanShoppingCart() {
        Long id = ThreadLocalUtil.getCurrentId();
        shoppingCartMapper.deleteByUserId(id);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart1 = BeanUtil.copyProperties(dto, ShoppingCart.class);
        shoppingCart1.setUserId(ThreadLocalUtil.getCurrentId());
        ShoppingCart shoppingCart2 = shoppingCartMapper.findExist(shoppingCart1);
        if(shoppingCart2.getNumber()==1){
            shoppingCartMapper.deleteByUser(shoppingCart2);
        }else {
            shoppingCart2.setNumber(shoppingCart2.getNumber()-1);
            shoppingCartMapper.updateNumber(shoppingCart2);

        }


    }


}
